package com.liuwenlin.zookeeper.zkclient.lock;

import com.liuwenlin.zookeeper.zkclient.config.ZKUtils;
import org.apache.zookeeper.ZooKeeper;

/**
 * @author 276834
 */
public class TestLock {

    public static void main(String[] args) {

        ZooKeeper zooKeeper = ZKUtils.getZooKeeperWithRootPath("/testLock");

        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    ZookeeperLock lock = new ZookeeperLock();
                    lock.setZkClient(zooKeeper);
                    lock.setThreadName(Thread.currentThread().getName());
                    try {
                        lock.tryLock();

                        //todo 具体业务
                        System.out.println("node " + lock.getPathName() + " is working...");

                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                    }
                }
            }, "thread-" + i);

            t.start();
        }
    }

}
